<?php

class Admin_Model_DbTable_Pointterrain extends Zend_Db_Table
{
    protected $_name = 'pointterrain';
    /**
		Recupererle point de la muraille par son id
     */
    public function getPointterrain($idpointterrain) {
        $idpointterrain = (int)$idpointterrain;
        $row = $this->fetchRow('idpointterrain = ' . $idpointterrain);
        if (!$row) {
            throw new Exception("Count not find row $idpointterrain");
        }
        return $row->toArray();    
    }
    
	public function getTerrain($idterrain) {
		$idterrain = (int)$idterrain;
		$row = $this->fetchAll('matricebr_idmatricebr = ' . $idterrain);
		return $row->toArray();    
	}

    public function addPointterrain($latitude, $longitude, $matricebr_idmatricebr, $pointterrain_idpointterrain_pred) {
        $data = array(
            'latitude' => $latitude,
            'longitude' => $longitude,
        	'matricebr_idmatricebr' => $matricebr_idmatricebr,
            'pointterrain_idpointterrain_pred' => $pointterrain_idpointterrain_pred,
        );
        $this->insert($data);
    }
    

    
    public function updatePointterrain($idpointterrain, $latitude, $longitude,  $matricebr_idmatricebr, $pointterrain_idpointterrain_pred) {
         $data = array(
            'latitude' => $latitude,
            'longitude' => $longitude,
         	'matricebr_idmatricebr' => $matricebr_idmatricebr,
            'pointterrain_idpointterrain_pred' => $pointterrain_idpointterrain_pred,
        );
        $this->update($data, 'idpointterrain = '. (int)$idpointterrain);
    }
    
    
	function deletePointterrain($idpointterrain) {
		$this->delete ( 'idpointterrain =' . ( int ) $idpointterrain );
	}
    /**
     * 
     * @return  
     */
    public function getAllPointterrain() {
		$all = $this->fetchAll('pointterrain_idpointterrain_pred=0');
		$point = array();
		$point['total'] = count($all->toArray());
		$point['rows'] = $all->toArray();
		$terrain = new Admin_Model_DbTable_Matricebr();
		$point['info'] = array();
		for($i=0;$i<count($all->toArray());$i++) {
			$point['info'][$i] = 
			$terrain->getTerrain($point['rows'][$i]['matricebr_idmatricebr']);
			$point['rows'][$i] = 
			$this->getTerrain($point['rows'][$i]['matricebr_idmatricebr']);
		}
		return $point;
    }
    
    public function getMatrice($matricebr_idmatricebr) {
        $matricebr_idmatricebr = (int)$matricebr_idmatricebr;
        $db = Zend_Db::factory('PDO_MYSQL', array ('host' => '127.0.0.1', 'username'
		 => 'root', 'password' => '@dmin2010gmv', 'dbname'   => 'gmv'));
		$select = $db->select()
		->from(array('p' => 'pointterrain'))
		->join(array('m' => 'matricebr'), 'm.id = p.matricebr_idmatricebr');
		$rows = $select->query()->fetchAll();
		$mat = array();
        $mat['total'] = count($rows);
        $mat['rows'] = $rows;
		return $mat;    
    }
    
	public function getCoordonneee($imin,$imax,$jmin,$jmax) {    
 		$select = $this ->select()
             -> from('pointterrain')
             -> where('i > ?', $imin)
             -> where('i <= ?', $imax)
             -> where('j > ?', $jmin)
             -> where('j <= ?', $jmax);

        $result = $this -> fetchAll($select);
        $res = array();
        $i = 0;
     	foreach ($result as $row) {
     		$res[$i] = array();
 			$res[$i] = $row->toArray();
 			$res[$i]["type"] = "pointterrain";
 			$i++;

		}
        return $res;
    }
}
